package com.snailhd.handler;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.snailhd.condition.StockSectionConditions;
import com.snailhd.entity.Stock;
import com.snailhd.vo.StockSectionVO;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.stream.Collectors;

/**
 * 区间统计数量
 */

@Slf4j
@Component
public class StockSectionHandler {

    public List<StockSectionVO> handle(List<Stock> list, StockSectionConditions conditions){
        if(null==conditions.getStart()){
            conditions.setStart(new BigDecimal(0));
        }

        TreeMap<Integer,Integer> map = new TreeMap<>();
        Integer maxSection = 0;
        if(conditions.getEnd()!=null){
            maxSection = conditions.getEnd().divide(conditions.getSectionVal(),BigDecimal.ROUND_UP).intValue();
        }

        for (Stock stock:list){
            HandeOneResult handeOneResult = handleOne(stock,conditions);
            if(null!=handeOneResult){
                Integer section = handeOneResult.getSection();
                if(maxSection<section){
                    maxSection = section;
                }
                Integer count = map.get(section);
                if(count==null){
                    count = 0;
                }
                map.put(section,++count);
            }
        }

        Map<Integer,String> sectionNames = new HashedMap();
        BigDecimal start = conditions.getStart();
        for (int i=0;i<maxSection;i++){
            BigDecimal end = start.add(conditions.getSectionVal());
            String sectionName = start.toString()+"--"+end.toString();
            start = end;
            sectionNames.put(i,sectionName);
            if(map.get(i)==null){
                map.put(i,0);
            }
        }

         return map.entrySet().stream().map(e-> new StockSectionVO(sectionNames.get(e.getKey()),e.getValue())).collect(Collectors.toList());
    }

    private HandeOneResult handleOne(Stock stock,StockSectionConditions conditions){

        JSONObject obj = JSON.parseObject(JSON.toJSONString(stock));
        BigDecimal fieldVal = obj.getBigDecimal(conditions.getField());
        log.info("-->{},fieldVal：{}",stock.getSymbol(),fieldVal);

        if(conditions.getStart().compareTo(fieldVal)>0){
            return null;
        }
        if(conditions.getEnd()!=null&& conditions.getEnd().compareTo(fieldVal)<0){
            return null;
        }

        int section = 0;
        BigDecimal start = conditions.getStart();
        while(true){
            BigDecimal end = start.add(conditions.getSectionVal());
            if(fieldVal.compareTo(end)<0){
                return new HandeOneResult(section);
            }
            start = end;
            section++;
        }
    }

    @Data
    class HandeOneResult{
        private int section;
        private String sectionName;

        public HandeOneResult(int section) {
            this.section = section;
        }
    }


}
